 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">权限规则</div>
        <div class="mr-30">
          <el-button size="medium" type="primary" @click="add">新 增</el-button>
        </div>
      </div>
      <div class="content-con">
        <el-form :model="form" ref="form" class="bank-search">
          <el-form-item prop="menu_name">
            <el-input v-model="form.menu_name" placeholder="请输入菜单名"></el-input>
          </el-form-item>
          <el-button class="save mb-22 ml-20" @click="search()">搜 索</el-button>
        </el-form>

        <div class="bank-form">
          <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              row-key="id"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }">
              <el-table-column prop="menu_name" label="菜单名" width="200">
              </el-table-column>
              <el-table-column prop="parent_name" label="父级" width="160">
              </el-table-column>
              <el-table-column prop="module" label="模块" width="160">
              </el-table-column>
              <el-table-column prop="controller" label="控制器名" width="160">
              </el-table-column>
              <el-table-column prop="action" label="方法名" width="160">
              </el-table-column>
              <el-table-column prop="router_name" label="路由" width="160">
              </el-table-column>
              <el-table-column prop="is_show" label="是否显示" width="160">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.is_show == 0" type="success">显示</el-tag>
                  <el-tag v-if="scope.row.is_show == 1" type="danger">不显示</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="320">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleEdit(scope.row)">
                    <i class="el-icon-circle-plus"></i>增加子菜单
                  </el-button>
                  <el-button type="text" @click="handleEdit(scope.row)">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页-页码 -->
          <div class="page">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.length">
            </el-pagination>
          </div>
        </div>
      </div>
      <rule-edit ref="RuleEdit" :rows="rows"></rule-edit>
   </el-card>

 </template>
<script>
import RuleEdit from '@/components/access-rule/RuleEdit';
export default {
  components:{
    RuleEdit
  },
  data () {
    return {
      rows: '',
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      tableData: [
        {
          id: '1',
          menu_name: '销售管理',
          parent_name: '顶级',
          module: 'api',
          controller: 'sale',
          action: 'index',
          router_name: 'SaleManage',
          is_show: 0,
          pid: '', //父级id
          params: '', //参数
          children: [
            {
              id: '30',
              menu_name: '新增销售订单',
              parent_name: '顶级',
              module: 'api',
              controller: 'sale.order',
              action: 'index',
              router_name: 'SaleOrder',
              is_show: 0,
            }
          ]
        }
      ],
      form: {
        menu_name: ''
      }
    }
  },

  methods: {
    //搜索
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    // 新增
    add(){
      this.$nextTick(()=>{
        this.$refs.RuleEdit.openAdd();
      });
    },
    
    //增加子菜单 需要获取到父级ID，其余为空
    //编辑操作
    handleEdit(row){
      this.rows = row;
      this.$nextTick(()=>{
        this.$refs.RuleEdit.openEdit();
      });
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
